﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace Tools
{
    public partial class FanYi : Base
    {
        public FanYi()
        {
            InitializeComponent();
            base.SetDefaultLocation();
        }

        string appid = "20180324000139554";
        string key = "0B67CRlk0VdYi4EjGZzF";

        /// <summary>
        /// 翻译
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)//翻译
        {
            try
            {
                var url = GetUrl(txt_str.Text, "auto", ((Button)sender).Tag.ToString());

                var lst = new List<string>();
                var content = Helper.Tools.Get(url);
                dynamic json = JsonConvert.DeserializeObject(content);
                foreach (var item in json.trans_result)
                {
                    lst.Add(item.dst.ToString());
                }
                txt_result.Text = string.Join(";", lst);
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误:" + ex.Message);
            }
        }

        /// <summary>
        /// 组装url
        /// </summary>
        /// <param name="q"></param>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <returns></returns>
        public string GetUrl(string q, string from, string to)
        {
            Random rd = new Random();
            var salt = rd.Next(10000, 1000000).ToString();

            var signStr = string.Format("{0}{1}{2}{3}", appid, q, salt, key);
            var sign = getMd5(signStr);

            var url = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
            var pars = new Dictionary<string, string>();
            pars.Add("appid", appid);
            pars.Add("q", q);
            pars.Add("from", from);
            pars.Add("to", to);
            pars.Add("salt", salt);
            pars.Add("sign", sign);

            var par = ""; var ls = "";
            foreach (var item in pars)
            {
                par += ls + string.Format("{0}={1}", item.Key, item.Value);
                ls = "&";
            }
            url += par;
            return url;
        }

        /// <summary>
        /// md5加密
        /// </summary>
        /// <param name="signStr"></param>
        /// <returns></returns>
        string getMd5(string signStr)
        {
            var md5 = new MD5CryptoServiceProvider();
            var result = Encoding.UTF8.GetBytes(signStr);
            var output = md5.ComputeHash(result);
            return BitConverter.ToString(output).Replace("-", "").ToLower();
        }

        private void txt_str_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
            {
                ((TextBox)sender).SelectAll();
            }
        }
    }
}
